package question2;


import question1.PilePleineException;
import question1.PileVideException;

import java.util.Vector;

/**
 * 
 * 
 * @author Marleix Mathieu
 * @version 0.1
 */

public class Pile3 implements PileI
{
 
  private Vector<Object> v;
  
  public Pile3(){
    this(0);
  }

  public Pile3(int taille){
		if (taille<=0) {
			v = new Vector<Object>(CAPACITE_PAR_DEFAUT);
		}
		else {
			v = new Vector<Object>(taille);
		}
  }
  
  public void empiler(Object o) throws PilePleineException{
	  if (estPleine()) throw new PilePleineException();
	  v.add(o);
  }
  
  public Object depiler() throws PileVideException{
		if (estVide()) throw new PileVideException();
		return v.remove(v.size()-1);
  }
  
  public Object sommet() throws PileVideException{
	if (estVide()) throw new PileVideException();
    return v.lastElement();
  }
  public int taille(){
    return v.size();
  }
  public int capacite(){
    return v.capacity();
  }
  public boolean estVide(){
    return v.isEmpty();
  }
  public boolean estPleine(){
    return this.capacite() == this.taille();
  }
  public String toString(){
		StringBuffer s = new StringBuffer();
		s.append("[");
		for (int i=v.size()-1;i>=0;i--) {
		      s.append(v.elementAt(i).toString());
		      if (i >0) s.append(", ");
		    }
		s.append("]");
		return s.toString();
  }
  
  public boolean equals(Object o){
	  return (Pile3)o != null && PileI.class.isInstance(o) && v.size() == ((Pile3)o).v.size()  && v.equals(((Pile3)o).v);
  }

  // fonction fournie
  public int hashCode(){
    return toString().hashCode();
  }

  // fonction fournie
  public Object clone() throws CloneNotSupportedException{
    Pile3 p = new Pile3(this.capacite());
    p.v = (Vector<Object>)v.clone();
    return p;
  }
}
